package com.sgcc.fj.pmos.e_fj_home.activity;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.arouter.common.CommonArouterPath;
import com.sgcc.fj.pmos.arouter.home.HomeArouterPath;
import com.sgcc.fj.pmos.common.base.MvpBaseActivity;
import com.sgcc.fj.pmos.common.http.DownloadUtil;
import com.sgcc.fj.pmos.common.utils.FileUtils;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.fj.pmos.e_fj_home.adapter.StructuralInformation1Adapter;
import com.sgcc.fj.pmos.e_fj_home.adapter.StructuralInformationAdapter;
import com.sgcc.fj.pmos.e_fj_home.bean.BasicInformationEnterpriseBean;
import com.sgcc.fj.pmos.e_fj_home.bean.SDBasicInformationEnterpriseBean;
import com.sgcc.fj.pmos.e_fj_home.contract.BasicInformationEnterpriseContract;
import com.sgcc.fj.pmos.e_fj_home.contract.SDBasicInformationEnterpriseContract;
import com.sgcc.fj.pmos.e_fj_home.presenter.BasicInformationEnterprisePresenter;
import com.sgcc.fj.pmos.e_fj_home.presenter.SDBasicInformationEnterprisePresenter;

import java.io.File;

/**
 * 企业基本信息------售电公司
 */
@Route(path = HomeArouterPath.HOME_SDBASICINFORMATIONENTERPRISE)
public class SDBasicInformationEnterpriseActivity extends MvpBaseActivity<SDBasicInformationEnterprisePresenter> implements SDBasicInformationEnterpriseContract.View {

    private TitleBar mTitleBar;
    private TextView mTvIndustrialInfo1;
    private TextView mTvIndustrialInfo2;
    private TextView mTvIndustrialInfo3;
    private TextView mTvIndustrialInfo4;
    private TextView mTvIndustrialInfo5;
    private TextView mTvIndustrialInfo6;
    private TextView mTvIndustrialInfo7;
    private TextView mTvIndustrialInfo8;
    private TextView mTvIndustrialInfo9;
    private TextView mTvIndustrialInfo10;
    private TextView mTvIndustrialInfo11;
    private TextView mTvIndustrialInfo12;
    private TextView mTvIndustrialInfo13;
    private TextView mTvContactInfo1;
    private TextView mTvContactInfo2;
    private TextView mTvContactInfo3;
    private TextView mTvContactInfo4;
    private TextView mTvBasicInfo1;
    private TextView mTvBasicInfo2;
    private TextView mTvBasicInfo3;
    private TextView mTvBasicInfo4;
    private String applyId = "", name = "";
    private String fileId;
    private RelativeLayout mRlFile;
    private RecyclerView mRvContent;
    private StructuralInformation1Adapter mStructuralInformation1Adapter;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_sdbasicinformationenterprise;
    }

    @Override
    protected void initViews() {
        super.initViews();
        applyId = getIntent().getStringExtra("guid");
        name = getIntent().getStringExtra("sourceName");
        findViewByIds();
        mPresenter.getData(applyId);
    }

    private void findViewByIds() {
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTvIndustrialInfo1 = (TextView) findViewById(R.id.tv_IndustrialInfo1);
        mTvIndustrialInfo2 = (TextView) findViewById(R.id.tv_IndustrialInfo2);
        mTvIndustrialInfo3 = (TextView) findViewById(R.id.tv_IndustrialInfo3);
        mTvIndustrialInfo4 = (TextView) findViewById(R.id.tv_IndustrialInfo4);
        mTvIndustrialInfo5 = (TextView) findViewById(R.id.tv_IndustrialInfo5);
        mTvIndustrialInfo6 = (TextView) findViewById(R.id.tv_IndustrialInfo6);
        mTvIndustrialInfo7 = (TextView) findViewById(R.id.tv_IndustrialInfo7);
        mTvIndustrialInfo8 = (TextView) findViewById(R.id.tv_IndustrialInfo8);
        mTvIndustrialInfo9 = (TextView) findViewById(R.id.tv_IndustrialInfo9);
        mTvIndustrialInfo10 = (TextView) findViewById(R.id.tv_IndustrialInfo10);
        mTvIndustrialInfo11 = (TextView) findViewById(R.id.tv_IndustrialInfo11);
        mTvIndustrialInfo12 = (TextView) findViewById(R.id.tv_IndustrialInfo12);
        mTvIndustrialInfo13 = (TextView) findViewById(R.id.tv_IndustrialInfo13);
        mTvContactInfo1 = (TextView) findViewById(R.id.tv_ContactInfo1);
        mTvContactInfo2 = (TextView) findViewById(R.id.tv_ContactInfo2);
        mTvContactInfo3 = (TextView) findViewById(R.id.tv_ContactInfo3);
        mTvContactInfo4 = (TextView) findViewById(R.id.tv_ContactInfo4);
        mTvBasicInfo1 = (TextView) findViewById(R.id.tv_BasicInfo1);
        mTvBasicInfo2 = (TextView) findViewById(R.id.tv_BasicInfo2);
        mTvBasicInfo3 = (TextView) findViewById(R.id.tv_BasicInfo3);
        mTvBasicInfo4 = (TextView) findViewById(R.id.tv_BasicInfo4);

        mRlFile = (RelativeLayout) findViewById(R.id.rl_file);
        mRvContent = (RecyclerView) findViewById(R.id.rv_content);

        mTitleBar.setTitle(name + "企业基本信息");

        mRlFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                openSystemFile(fileId);
            }
        });

        LinearLayoutManager secondLLManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRvContent.setLayoutManager(secondLLManager);
        mRvContent.setItemAnimator(new DefaultItemAnimator());
        mStructuralInformation1Adapter = new StructuralInformation1Adapter(this);
        mRvContent.setAdapter(mStructuralInformation1Adapter);
    }

    @Override
    protected SDBasicInformationEnterprisePresenter createPresenter() {
        return new SDBasicInformationEnterprisePresenter();
    }

    @Override
    public void getDataSuccess(SDBasicInformationEnterpriseBean response) {
        if (response.getData().getIpGhSdCompany() != null) {
            SDBasicInformationEnterpriseBean.DataBean.IpGhSdCompanyBean bean = response.getData().getIpGhSdCompany();
            mTvIndustrialInfo1.setText(bean.getCreditcode());
            mTvIndustrialInfo2.setText(bean.getMembersName());
            mTvIndustrialInfo4.setText(bean.getLegalReprName());
            mTvIndustrialInfo5.setText(bean.getEnterpriseType());
            mTvIndustrialInfo6.setText(bean.getFounddate());

            mTvContactInfo1.setText(bean.getContactName());
            mTvContactInfo2.setText(bean.getOfficePhone());
            mTvContactInfo3.setText(bean.getEmail());
            mTvContactInfo4.setText(bean.getFax());

            String saleType = "";
            switch (bean.getSaleType()) {
                case "2":
                    saleType = "独立售电公司";
                    break;
            }

            mTvBasicInfo1.setText(saleType);
            mTvBasicInfo2.setText(bean.getEnergyUsed());
            mTvBasicInfo3.setText(bean.getTotalAsset());

            fileId = bean.getXycns();

        }
        if (response.getData().getBusinessLicenseForm() != null) {
            SDBasicInformationEnterpriseBean.DataBean.BusinessLicenseFormBean bean = response.getData().getBusinessLicenseForm();
            mTvIndustrialInfo3.setText(bean.getRegistrationNumber());
            mTvIndustrialInfo7.setText(bean.getRegcapital());
            mTvIndustrialInfo8.setText(bean.getApprovedDate());
            mTvIndustrialInfo9.setText(bean.getRegistrationAuthority());
            mTvIndustrialInfo10.setText(bean.getRegistrationStatus());
            mTvIndustrialInfo11.setText(bean.getAddress());
            mTvIndustrialInfo12.setText(bean.getScope());
            mTvIndustrialInfo13.setText(bean.getBusinessterm());
        }
        if (response.getData().getIpGhGudong() != null) {
            mStructuralInformation1Adapter.addData(response.getData().getIpGhGudong());
        }
    }

    @Override
    public void getDataFailed() {
        ToastUtils.showShort("数据请求失败");
    }

    /**
     * 下载并调用系统方式打开文件
     *
     * @param fileId
     */
    public void openSystemFile(String fileId) {
        showLoading();
        String webUrl = "https://pmos.fj.sgcc.com.cn/px-settlement-infpubmeex/fileService/preview?fileId=" + fileId + "#toolbar=0";
        DownloadUtil.get().downloadGET(mContext, webUrl,
                mContext.getExternalFilesDir("pdf"),
                new DownloadUtil.OnDownloadListener() {
                    @Override
                    public void onDownloadSuccess(File file) {
                        dismissLoading();
                        String fileType = getFileExtension(file);
                        if ("pdf".toLowerCase().equals(fileType.toLowerCase())) {
                            ARouter.getInstance()
                                    .build(CommonArouterPath.COMMON_PDFPREVIEW)
                                    .withString("mFilePath", file.getPath())
                                    .navigation();
                        } else {
                            FileUtils.openFileByPath(mContext, file.getPath());
                        }
                    }

                    @Override
                    public void onDownloading(int progress) {
                    }

                    @Override
                    public void onDownloadFailed(Exception e) {
                        dismissLoading();
                        ToastUtils.showShort("文件加载失败");
                    }
                });
    }

    /**
     * 获取文件扩展名
     *
     * @param file
     * @return
     */
    private String getFileExtension(File file) {
        String fileName = file.getName();
        if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        else return "";
    }


}
